﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
   /// <summary>
   /// 用户数据字典DAL
   /// </summary>
    public class UsrDataDictionary
    {
        /// <summary>
        /// 得到一个实体类
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fTableName">表名</param>
        /// <param name="fFieldName">字段名</param>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <returns>返回数据字典信息</returns>
        public Model.UsrDataDictionary GetModel(string fTableName, string fFieldName, string fEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FTableName,FFieldName,FSysAttribute,FUserAttribute,FIsShowGrid,FInputMethod,FIsRequire,FIsShowForm,FEntityNo from t_UsrDataDictionary ");
            strSql.Append(" where FTableName=@FTableName and FFieldName=@FFieldName and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FFieldName", SqlDbType.VarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = fTableName;
            parameters[1].Value = fFieldName;
            parameters[2].Value = fEntityNo;

            Model.UsrDataDictionary model = new Model.UsrDataDictionary();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FTableName"] != null && ds.Tables[0].Rows[0]["FTableName"].ToString() != "")
                {
                    model.FTableName = ds.Tables[0].Rows[0]["FTableName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FFieldName"] != null && ds.Tables[0].Rows[0]["FFieldName"].ToString() != "")
                {
                    model.FFieldName = ds.Tables[0].Rows[0]["FFieldName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FSysAttribute"] != null && ds.Tables[0].Rows[0]["FSysAttribute"].ToString() != "")
                {
                    model.FSysAttribute = ds.Tables[0].Rows[0]["FSysAttribute"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FUserAttribute"] != null && ds.Tables[0].Rows[0]["FUserAttribute"].ToString() != "")
                {
                    model.FUserAttribute = ds.Tables[0].Rows[0]["FUserAttribute"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FIsShowGrid"] != null && ds.Tables[0].Rows[0]["FIsShowGrid"].ToString() != "")
                {
                    model.FIsShowGrid = ds.Tables[0].Rows[0]["FIsShowGrid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FInputMethod"] != null && ds.Tables[0].Rows[0]["FInputMethod"].ToString() != "")
                {
                    model.FInputMethod = ds.Tables[0].Rows[0]["FInputMethod"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FIsRequire"] != null && ds.Tables[0].Rows[0]["FIsRequire"].ToString() != "")
                {
                    model.FIsRequire = ds.Tables[0].Rows[0]["FIsRequire"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FIsShowForm"] != null && ds.Tables[0].Rows[0]["FIsShowForm"].ToString() != "")
                {
                    model.FIsShowForm = ds.Tables[0].Rows[0]["FIsShowForm"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FEntityNo"] != null && ds.Tables[0].Rows[0]["FEntityNo"].ToString() != "")
                {
                    model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="model">数据字典信息</param>
        public void UpdateIsShowGrid(Model.UsrDataDictionary model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_UsrDataDictionary set ");
            strSql.Append("FIsShowGrid=@FIsShowGrid");
            strSql.Append(" where FTableName=@FTableName and FFieldName=@FFieldName and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FFieldName", SqlDbType.VarChar,30),
					new SqlParameter("@FIsShowGrid", SqlDbType.VarChar,1),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FTableName;
            parameters[1].Value = model.FFieldName;
            parameters[2].Value = model.FIsShowGrid;
            parameters[3].Value = model.FEntityNo;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="model">数据字典信息</param>
        /// <returns>是否增加成功</returns>
        public bool Add(Model.UsrDataDictionary model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_UsrDataDictionary(");
            strSql.Append("FTableName,FFieldName,FSysAttribute,FUserAttribute,FIsShowGrid,FInputMethod,FIsRequire,FIsShowForm,FEntityNo)");
            strSql.Append(" values (");
            strSql.Append("@FTableName,@FFieldName,@FSysAttribute,@FUserAttribute,@FIsShowGrid,@FInputMethod,@FIsRequire,@FIsShowForm,@FEntityNo)");
            SqlParameter[] parameters = {
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FFieldName", SqlDbType.VarChar,30),
					new SqlParameter("@FSysAttribute", SqlDbType.NVarChar,60),
					new SqlParameter("@FUserAttribute", SqlDbType.NVarChar,60),
					new SqlParameter("@FIsShowGrid", SqlDbType.VarChar,1),
					new SqlParameter("@FInputMethod", SqlDbType.VarChar,1),
					new SqlParameter("@FIsRequire", SqlDbType.VarChar,1),
					new SqlParameter("@FIsShowForm", SqlDbType.VarChar,1),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FTableName;
            parameters[1].Value = model.FFieldName;
            parameters[2].Value = model.FSysAttribute;
            parameters[3].Value = model.FUserAttribute;
            parameters[4].Value = model.FIsShowGrid;
            parameters[5].Value = model.FInputMethod;
            parameters[6].Value = model.FIsRequire;
            parameters[7].Value = model.FIsShowForm;
            parameters[8].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
